<template>
    <block v-if="scene == 1154">
    	<pyq-open-chunk></pyq-open-chunk>
    </block>
    <block v-else>
        <view class="sunshinePage bg-fff">
            <block v-if="kaiList.length > 0">
                <up-tabs :list="kaiList" :current="currKai" lineColor="#007235" keyName="title" @click="changeKai"></up-tabs>

                <!-- <scroll-view :scroll-x="true" class="kaiNav top-20">
                    <block v-for="(kai, index) in kaiList" :key="index">
                        <view class="kaiItem" :class="currKai === index?'active':''" @click="changeKai(index)">
                            {{kai.title}}
                        </view>
                    </block>
                </scroll-view> -->
                
                <view class="listBox" v-if="kaiList[currKai].list.length > 0">
                    <view class="item" v-for="(item, index) in kaiList[currKai].list" :key="index">
                        <view class="imageBox" @click="previewImages(index)">
                            <image class="sunImage" :src="item.image[0].url" :lazy-load="true" mode="widthFix" />
                        </view>
                        <view class="sunshineTitle txtHidden font14">{{item.title}}</view>
                    </view>
                    <!-- <share-btn :display="scrollAnimate"></share-btn> -->
                </view>

                <up-empty text="暂无数据" mode="list" marginTop="100" v-if="kaiList[currKai].list.length === 0"></up-empty>

                <block v-if="kaiList[currKai].list.length > 0">
                    <view class="empty flex font13 col-grey">暂无更多</view>
                </block>

                <u-gap height="40" bg-color="#fff"></u-gap>
            </block>

            <up-empty text="暂无数据" mode="list" marginTop="100" v-if="kaiList.length === 0"></up-empty>

			<!-- <bind-seller-bottom-bar :detail="recommendAdviser" v-if="recommendAdviser&&recommendAdviser.avatar_url"></bind-seller-bottom-bar> -->
        </view>
    </block>
</template>
<script>
    import { mapState, mapActions } from 'pinia';
    import { useConfigStore, useUserInfoStore } from '@/store/index';
    import { isBoolean } from '@/common/common';
	import { panListAPI, getSunshineList } from "@/common/api";
    // import { dispatchClient } from '@/common/api-adviser';
    // import { getSellerInfoById } from '@/common/api/adviser';

	export default {
		data() {
			return {
                productId: '',
                kaiId: '',
                kaiList: [],
                currKai: 0,
				title: '',
                share: false,
                start_time: 0,
                scrollAnimate: false,
                recommendAdviser: {}
            }
		},
        onShareAppMessage(res) {
            return {
                title: `【${this.title}】-日照分析`,
                path: `/pages/product/sunshine/index?productId=${this.productId}&kaiId=${this.kaiId}&puid=${this.userInfo.id}&share=true&title=${this.title}&prepage=楼盘日照分析-${this.productId}-${this.title}`,
            }
        },
        
        //#ifdef MP-WEIXIN
        onShareTimeline() {
            return {
                title: `【${this.title}】-日照分析`,
                query: `productId=${this.productId}&kaiId=${this.kaiId}&puid=${this.userInfo.id}&share=true&title=${this.title}&prepage=楼盘日照分析-${this.productId}-${this.title}`
            }
        },
        //#endif
        
		onLoad(options) {
            const that = this;
            let { productId='', kaiId='', puid='', title='', share=false } = options;
            
            //朋友圈进入小程序快照场景
            if(this.scene == 1154) return;
            
            this.productId = productId&&productId!=='undefined'?productId:'';
            this.kaiId = kaiId&&kaiId!=='undefined'?kaiId:'';
			this.title = title&&title!=='undefined'?title:'';
            this.share = isBoolean(share);
            uni.setNavigationBarTitle({
                title: `${title}日照分析`
            });
            if(puid&&puid!=='undefined') {
                this.setPuid(puid)
            }
            
            if(this.productId){
            	uni.$check_and_login(function () {
                    that.init()
            	})
            }
		},
        
        computed:{
            ...mapState(useConfigStore, ['scene', 'puid']),
            ...mapState(useUserInfoStore, ['userInfo']),
        },

		methods: {
            ...mapActions(useConfigStore, ['setPuid']),

            async init() {
                //绑定客户
                // await dispatchClient({
                //     share: this.share,
                //     puid: this.puid,
                //     pid: this.productId
                // })

                this.getPanList();

                //根据顾问ID获取被绑定的顾问信息
                // if(this.puid && this.puid !== this.userInfo.id) {
                //     let sellInfoRes = await getSellerInfoById({id: this.puid, productId: this.productId});
                //     if(sellInfoRes.status === 0) {
                //         if(sellInfoRes.detail.product_id && sellInfoRes.detail.product_id == this.productId) {
                //             this.recommendAdviser = sellInfoRes.detail;
                //         }
                //     }
                // }
            },
            
            //获取开盘id
            getPanList(){
                uni.showLoading({
                	title: '数据加载中',
                	mask: true
                })
                panListAPI(this.productId).then(res => {
                    if(res.status === 0) {
                        if(res.detail.length > 0) {
                            this.kaiList = res.detail.map(item => ({
                                ...item,
                                list: [],
                            }))
                            if(this.kaiId) {
                                this.kaiList.forEach((kai, index) => {
                                    if(kai.id == this.kaiId) {
                                        this.currKai = index
                                    }
                                })
                            }
                            this.getSunshineImages();
                        }
                        uni.hideLoading();
                    }else {
                        uni.hideLoading();
                        uni.$u.toast(res.detail)
                    }
                })
            },
            
            //获取开盘对应的日照
			getSunshineImages(){
                let currKaiItem = this.kaiList[this.currKai];
                if(currKaiItem.list.length > 0) {
                    return
                }
                getSunshineList(this.kaiId?this.kaiId:currKaiItem.id).then(res => {
                	if(res.status == 0){
                        if(res.detail.length > 0) {
                            currKaiItem.list = res.detail;
                        }
                        uni.hideLoading()
                	}else{
                        uni.hideLoading();
                        uni.$u.toast(res.detail);
                    }
                })
			},
            
            //切换开盘
            changeKai({index}){
                if(index === this.currKai) {
                    return
                }
                this.currKai = index;
                this.kaiId = this.kaiList[index].id;
                this.getSunshineImages()
            },
			
			// 预览图片
			previewImages(index){
				if(this.kaiList[this.currKai].list[index].url){
					let url = encodeURIComponent(this.kaiList[this.currKai].list[index].url)
					uni.navigateTo({
						url: `/pages/house/article-item?url=${url}&title=${this.kaiList[this.currKai].list[index].title}&productId=${this.kaiList[this.currKai].list[index].id}&puid=${this.puid}`,
					})
				}else{
					uni.previewImage({
						current: index,
						urls: this.kaiList[this.currKai].list.map(item => (item.image[0].url)),
						longPressActions: {
							itemList: ['发送给朋友', '保存图片', '收藏'],
							success: function(data) {
							},
							fail: function(err) {
							}
						}
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.sunshinePage{
        min-height: 100vh;
        overflow: hidden;
        .listBox{
            padding: 0rpx 28rpx;
        }
		.item{
			width: 100%;
			margin-top: 20rpx;
			.imageBox{
				width: 100%;
				text-align: left;
				.sunImage{
					width: 100%;
				}
			}
			.sunshineTitle{
				font-size: 28rpx;
			}
		}
		.noData{
			width: 100%;
			height: 100vh;
		}
	}
</style>
